/******************************************************
 Project:				PCSync
 Author:				jean-philippe
 File name:				Run.java
 Date created:			Aug 17, 2012
 Date last modified:	Aug 21, 2012
 *******************************************************
 Modification History
 *******************************************************
 Aug 17, 2012		Initial version.
 Aug 21, 2012		Moved directory polling to the
 					WatcherThread class.
 ******************************************************/
package pcsync.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import pcsync.Config;
import pcsync.MessageAuthenticate;

public class Client
{
	private Logger logger;
	private ArrayList<Thread> threads;
	
	public Client()
	{
		this.initLogging();
		this.initWatcherThreads();
		this.startPolling();
		this.connectToServer();
	}
	
	/**
	 * Sets up the logger and its configuration.
	 */
	private void initLogging()
	{
		// Enable logging
		PropertyConfigurator.configure(Config.LOGGER_PROPERTIES_FILE);
		logger = Logger.getLogger(this.getClass().getCanonicalName());
		logger.info("Application started");
	}
	
	/**
	 * Initializes the watcher thread with the directory list.
	 */
	private void initWatcherThreads()
	{
		// Load directories
		DirectoryLoader dirLoader = new DirectoryLoader(Config.DIRECTORIES_FILE);
		
		// Create WatcherThread which will poll the directories
		Iterator<Path> iter = dirLoader.iterator();
		WatcherThread wt = new WatcherThread();
		while(iter.hasNext()) 
		{
			wt.add(iter.next());
		}
		
		// Create the Thread and add it to the threads list
		this.threads = new ArrayList<Thread>();
		Thread t = new Thread(wt);
		t.setName("watcher");
		threads.add(t);
	}
	
	private void connectToServer()
	{
		try
		{
			Socket server = new Socket("centremedia", 4444);
			PrintWriter out = new PrintWriter(server.getOutputStream(), true);
			BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));
			logger.debug("Connected to " + server.getInetAddress().toString());
			
			MessageAuthenticate msgAuth = new MessageAuthenticate();
			msgAuth.setUsername("jp");
			msgAuth.setPassword("qwerty");
			
			ObjectOutputStream messageOutput = new ObjectOutputStream(server.getOutputStream());
			messageOutput.writeObject(msgAuth);
			
			String fromServer;
			while( (fromServer = in.readLine()) != null) {
				logger.debug("Server: " + fromServer);
				String fromUser = "bla";
				logger.debug("Client: " + fromUser);
				out.println(fromUser); 
			}
			
			/*
			String fromUser = "bla";
			logger.debug("Client: " + fromUser);
			out.println(fromUser); 
			*/
		}
		catch (UnknownHostException e)
		{
			// TODO Auto-generated catch block
			logger.error("");
			e.printStackTrace();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			logger.error("");
			e.printStackTrace();
		}
	}
	
	/**
	 * Starts all watcher threads.
	 */
	private void startPolling()
	{
		Iterator<Thread> iter = threads.iterator();
		while (iter.hasNext())
		{
			iter.next().start();
		}
	}
	
	/**
	 * Launch the Run class which starts the application.
	 * @param args
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public static void main(String[] args) throws IOException, InterruptedException
	{
		new Client();
	}
}
